CFLAGS = -Wall -pedantic -ansi

ALL := main.out
sources := $(wildcard *.c) 

#all
all: $(ALL)
.PHONY: all

# rules
main.out: $(filter-out main.o, $(sources:.c=.o)) main.o
	gcc $(CFLAGS) -o $@ $^

#.d generation
%.d: %.c
	@set -e; rm -f $@; \
	gcc -MM $< > $@.$$$$; \
	sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ >$@; \
	rm -f $@.$$$$

# include all the .d files
include $(sources:.c=.d)

# clean action
.PHONY: clean cleanOut cleanObj cleanDep
clean: cleanDep cleanObj cleanOut

cleanDep:
	-rm *.d

cleanObj:
	-rm *.o

cleanOut:
	-rm *.out

